<?php

/*
 * 这不是一个开放软件，您不能用于商业应用
 * 不允许对程序代码以任何形式任何目的的再发布。
 * Copyright (c) 2015 YOUM.Co.Ltd. All rights reserved..
 * 网站地址：http://www.huishuojs.com
 * Author Zhyu
 */

/**
 * @author Zhyu
 * @date 2016-01-19 04:44:13
 * @copyright 杭州晖硕有码科技
 */
class System_zhuan extends PC_Controller{
    protected $go_url = '/admin/system_zhuan/index';
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('system_money_zhuan_log_model');
        $this->load->model('user_model');
    }
    public function index(){
    	$this->data['fromusername'] =  isset($_POST['fromusername'])? $_POST['fromusername'] : '';
    	$this->data['tousername'] =  isset($_REQUEST['tousername'])? $_REQUEST['tousername'] : '';
    	$this->data['select_type']=  isset($_POST['select_type'])?$_POST['select_type']:'or';
    	$this->data['start_time']= isset($_POST['start_time'])?$_POST['start_time']:'';
    	$this->data['end_time']= isset($_POST['end_time'])?$_POST['end_time']:'';
        $message=$this->uri->segment(4)?$this->uri->segment(4):'1';
        $this->data['message']=$message;
        $this->data['error']    =  empty($_GET['message'])?'':$_GET['message'];
        $this->load->view('/admin/system_zhuan',$this->data);
    }
    
     /**
     * ajax 获取数据
     */
    public function ajax_system_zhuan_log_list(){
       $data=$_GET;
        $start = $data['iDisplayStart']; //显示的起始索引
        $length = $data['iDisplayLength'];//显示的行数
        $sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列
        $sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
        $search = $data ['sSearch'];//全局搜索字段

        $where = '1=1 ';
        if(isset($data['user_id'])){
        	$where.=' and from_user_id='.$data['user_id'];
        }
        if ($this->admin ['admin_type'] == 'user') {
        	$where.=' and from_user_id='.$this->admin['id'];
        }
		$userid_where = '';
        if ($this->admin ['admin_type'] == 'admin') {
        	$userid_where = '';
        	if (isset ( $data ['fromusername'] ) && $data ['fromusername']) {
        		$sql = "SELECT id FROM user WHERE locate ('" . $data ['fromusername'] . "' , user.username) > 0";
        		$query = $this->db->query ( $sql );
        		$user_ids = array ();
        		foreach ( $query->result () as $row ) {
        			$user_ids [] = $row->id;
        		}
        		$userid_where = " from_user_id in(" . implode ( ',', $user_ids ) . ")";
        		unset ( $data ['fromusername'] );
        	}
        }
        if (isset ( $data ['tousername'] ) && $data ['tousername']) {
        	$sql = "SELECT id FROM user WHERE user.username ='" . $data ['tousername'] . "' ";
        	$query = $this->db->query ( $sql );
        	$touser_ids = array ();
        	foreach ( $query->result () as $row ) {
        		$touser_ids [] = $row->id;
        	}
        	$where.= " and  to_user_id in(" . implode ( ',', $touser_ids ) . ")";
        	unset ( $data ['tousername'] );
        }
        $data_where = '';
        if (isset ( $data ['start_time'] ) && $data ['start_time']) {
        	$data_where = 'create_time >=' . strtotime ( $data ['start_time'] );
        	unset ( $data ['start_time'] );
        }
        if (isset ( $data ['end_time'] ) && $data ['end_time']) {
        	$data_where .= ' and create_time <=' . strtotime ( $data ['end_time'] );
        	unset ( $data ['end_time'] );
        }
        if ($data_where && $userid_where) {
        	$where .= 'and (' . $userid_where . ' ' . $data ['select_type'] . ' ' . $data_where . ')';
        } else if ($data_where) {
        	$where .= ' and ' . $data_where;
        } else if ($userid_where) {
        	$where .= ' and ' . $userid_where;
        }
        
        
        $this->data['count'] = $this->system_money_zhuan_log_model->getCount($where);
        $aaData=array();
        if($this->data['count']){
            $order_by = $sort_th." ".$sort_type;
            $system_money_log_list = $this->system_money_zhuan_log_model->getSystemMoneyZhuanLogList($where,$length,$start,$order_by);
            //$this->load->model('user_model');
            $users=array();
            $result=array();
            
            foreach($system_money_log_list as $key=>$value){
                $shenhe_url='';

                if(isset($users[$value['from_user_id']])){
                    $value['from_true_name']=$users[$value['from_user_id']]['true_name'];
                }
                $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                $user_info=$this->user_model->checkUser(array('id'=>$value['from_user_id']),'true_name');
                if($user_info){
                    $value['from_true_name']=$user_info['true_name'];
                    $users[$value['from_user_id']]['true_name']=$user_info['true_name'];
                }
                if(isset($users[$value['to_user_id']])){
                    $value['true_name']=$users[$value['to_user_id']]['true_name'];
                }
                $user_info=$this->user_model->checkUser(array('id'=>$value['to_user_id']),'true_name');
                if($user_info){
                    $value['to_true_name']=$user_info['true_name'];
                    $users[$value['to_user_id']]['true_name']=$user_info['true_name'];
                }
                $value['kou']=$value['kou']=='system'?'报单币':'余额';
                //$value['operate']=$shenhe_url;
                $result[]=$value;
            }
               
            $aaData=$result;
            
        }
        $output['aaData'] = $aaData;
        $output['sEcho'] = $_GET['sEcho'];
        $output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
        $output['iTotalRecords'] = $this->data['count']; //总共有几条数据
        echo json_encode($output); //最后把数据以json格式返回
    }
     /**
     *提现
     */
    public function add_page(){
        if(isset($_GET['user_id'])){
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('id'=>$_GET['user_id'],'username,true_name,balance,system_balance'));
            $this->data['true_name']=$user['true_name'];
            $this->data['username']=$user['username'];
            $this->data['balance']=$user['balance'];
            $this->data['user_id']=$user['id'];
            $this->data['system_balance']=$user['system_balance'];
            $this->load->view('/admin/system_zhuan_add.php',  $this->data);
        }else{
            $this->location_href("/admin/user/user_list/6?parent_id=".$_GET['user_id']);
        }
    }
    /**
     * 添加申请提现记录
     */
    public function add(){
        $message=false;
        if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['to_user_id'])){
            $message='参数错误';
            
        }else{
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']),'id,username,password2,true_name,money,freeze,balance,system_money,system_balance');
            if(empty($user)){
                $message='用户不存在';
            }else if($this->admin['admin_type']=='user' && ($user['system_balance']-$_POST['money'])<0){
					 $message='余额不足';
				}else{
				
                if($user['password2']==md5($_POST['password'])){
                     $touser=$this->user_model->checkUser(array('id'=>$_POST['to_user_id']),'id,username,true_name,money,freeze,balance,system_money,system_balance');
                    //$tax=$_POST['money']*0.08;
                    $feiyong=$_POST['money']*ZHUANZHANG;
                    $data['from_user_id']=$user['id'];
                    $data['to_user_id']  =$_POST['to_user_id'];
                    $data['money']  =$_POST['money'];
                    $data['feiyong'] =$feiyong;
                    $data['shiji_money']=$_POST['money']-$feiyong;
                    $data['remark']=$_POST['remark'];
                    $data['kou']    =$_POST['kou'];
                    $data['remark']=$_POST['remark'];
                    $data['create_time']=time();
                    
                    $result=$this->system_money_zhuan_log_model->addSystemMoneyZhuanLog($data);
                    if($result){
                        $update_data['freeze']=$user['freeze']+$_POST['money'];
						$to_user_data=array();
                        if($_POST['kou']=='system'){
                            $update_data['system_balance']=$user['system_balance']-$_POST['money'];
							$to_user_data['system_balance']=$touser['system_balance']+$data['shiji_money'];
                        }
						/*else{
                            $update_data['balance']=$user['balance']-$_POST['money'];
							$to_user_data['balance']=$touser['balance']+$_POST['money'];
                        }*/
                        $update=$this->user_model->editUser(array('id'=>$user['id']),$update_data);
                        
                       // $to_user_data['balance']=$touser['balance']+$_POST['money'];
                        $update=$this->user_model->editUser(array('id'=>$_POST['to_user_id']),$to_user_data);
                        //$update=true;
                        if($update){
                            $logdata[]=array(
                                'user_id'=>$user['id'],
                                'username'=>$user['username'],
                                'true_name'=>$user['true_name'],
                                'money'=>'-'.$_POST['money'],
                                'type' =>'zhuan',
                                'remark'=>$user['true_name'].'您于'.date('Y-m-d H:i:s',time())."给".$touser['username'].'转账'.$_POST['money']."元,扣除费用".$feiyong.'实际到账'.$data['shiji_money'],
                                'create_time'=>  time()
                            );
                            $logdata[]=array(
                                'user_id'=>$touser['id'],
                                'username'=>$touser['username'],
                                'true_name'=>$touser['true_name'],
                                'money'=>$data['shiji_money'],
                                'type' =>'zhuan',
                                'remark'=>$touser['username'].'您收到一笔来自'.$user['username'].'的转账，金额是'.$data['shiji_money'].'元。余额'.$to_user_data['system_balance'].'备注'.$_POST['remark'],
                                'create_time'=>  time()
                            );
                            if($_POST['kou']=='system'){
                                $this->add_system_money_log($logdata);
                            }else{
                                $this->add_money_log($logdata);
                            }
                           
                            
                           $this->location_href($this->go_url."/4");exit;
                        }else{
                            $this->system_money_zhuan_log_model->deleteSystemMoneyZhuanLog(array('id'=>$result));
                            $message='添加失败';
                        }
                    }
                }else{
                    $message='交易密码错误';
                }
            }
        }
        $this->location_href($this->go_url."/5?message=".$message);
    }
    /**
     * 检测用户是否正确
     */
    public function check_user(){
        $return =array();
        $return['success']='0';
        if($_POST['username']){
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']));
            if($user ){
                if($user['id']!=$_POST['from_user_id']){
                    $return['user']=$user;
                    $return['success']='1';
                }else{
                    $return['error']='不能给自己转账';
                }
            }else{
                $return['error']='用户不存在';
            }
        }else{
            $return['error']='用户不存在';
        }
        echo json_encode($return);
    }
}
